# 1.复刻课上的案例爬取房天下这个网站(https://www.fang.com/)
# 要求
# <1>爬取自己相对应城市的新房信息
# <2>爬取前10页的新房信息即可
#  <3>将爬以的信息保存到csv文件


import requests
from lxml import etree

def ftx(page):
    # 1.获取网页源代码
    url = f"https://wuhan.newhouse.fang.com/house/s/b9{page}/" #全球统一资源定位符
    # 请求头，模拟浏览器
    headers = {
        'Referer': 'https://wuhan.newhouse.fang.com/',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.46'
    }
    # r就是源代码
    r = requests.get(url=url,headers =headers).content.decode()
    # 2.解析源代码获取数据
    ret = etree.HTML(r)
    # 找到所有的li标签
    li_list = ret.xpath('//div[@id="newhouse_loupan_list"]/ul/li')
    print(li_list)
    for li in li_list:
            # 获取楼盘名字
        title = li.xpath('.//div[@class="nlcd_name"]/a/text()')[0].strip()
        print(title)
        # 楼盘大小
        size = li.xpath('.//div[@class="house_type clearfix"]/text()')
        size = ''.join(size).replace('\n', '').replace('\t', '')
        # 地址
        addres = li.xpath('.//div[@class="address"]/a/@title')[0]
        addres = addres.replace(',', '，')
        # 房源
        fangyuan = li.xpath('.//div[@class="fangyuan"]//text()')
        fangyuan = '|'.join(fangyuan).replace('\n', '').replace('\t', '')
        # 售价
        price = li.xpath('.//div[@class="nhouse_price"]')
        if price:
            price = li.xpath('.//div[@class="nhouse_price"]')[0].xpath('string(.)').strip()
        else:
            price = '已售完'
        # 电话
        phe = li.xpath('.//div[@class="tel"]/p//text()')
        phe = ''.join(phe).replace('\n', '').replace('\t', '')
        print(title, size, addres, fangyuan, price, phe)
        f.write(f'{title},{size},{addres},{fangyuan},{price},{phe}\n')
    # 3.保存数据
if __name__ == '__main__':
    with open('房天下.csv', 'w', encoding='utf-8-sig') as f:
        f.write('楼盘,大小,地址,房源,价格,电话\n')
        for page in range(1, 11):
            ftx(page)
            print(f'第{page}页')


